package gov.mogaha.ntis.cmm.mob.law.adapter;

import gov.mogaha.ntis.cmm.mob.law.util.CmmMobLawStr;
import gov.mogaha.ntis.cmm.mob.law.vo.CmmMobLawVO;
import gov.mogaha.ntis.cmm.mob.law.wrapper.CmmMobLawListWrapper;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CmmMobLawListAdapter extends ArrayAdapter<CmmMobLawVO>{
	Context cxt;
	int resId;
	ArrayList <CmmMobLawVO> list;

	public CmmMobLawListAdapter(Context cxt, int resId, ArrayList<CmmMobLawVO> list) {
		super(cxt, resId, list);
		this.cxt = cxt;
		this.resId = resId;
		this.list = list;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		CmmMobLawListWrapper wrapper;
		
		if(convertView == null){
			
			LayoutInflater vi = (LayoutInflater) cxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			convertView = vi.inflate(resId, null);
		
			wrapper = new CmmMobLawListWrapper(convertView);
			
			convertView.setTag(wrapper);
		}
		
		CmmMobLawVO vo = list.get(position);
		
		if( vo!= null){
			wrapper = (CmmMobLawListWrapper) convertView.getTag();
			
			TextView lawSnoView = wrapper.getLawSnoView();
			TextView lawNmView = wrapper.getLawNmView();
			TextView lawTypeNmView = wrapper.getLawTypeNmView();
			TextView lawYmdView = wrapper.getLawYmdView();
			
			lawSnoView.setText(String.valueOf(position+1));
			lawNmView.setText(vo.lawNm);
			lawTypeNmView.setText(vo.lawTypeNm+" 제"+vo.lawNo+"호");
			try{
				lawYmdView.setText(String.format(CmmMobLawStr._DATE_EXP, vo.lawYmd.substring(0,4), vo.lawYmd.substring(4,6), vo.lawYmd.substring(6,8)));
			}catch (IndexOutOfBoundsException idx) {
				Log.e("error",CmmMobLawListAdapter.class.getName() + " getView Error");
			}
			
		}
		
		return convertView;
	}

}
